package com.guli.eduCrm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonUtils.R;
import com.guli.eduCrm.entity.CrmBanner;
import com.guli.eduCrm.service.impl.CrmBannerServiceImpl;
import com.guli.serviceBase.Entity.GuliExcepiton;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-11
 */
@Api(description = "banner后台管理")
@RestController
@ResponseBody
@RequestMapping("/eduCrm/crmBanner/admin")
//@CrossOrigin // 解决跨域问题
public class CrmBannerAdminController {
    @Autowired
    private CrmBannerServiceImpl crmBannerService;

    @ApiOperation(value = "分页显示条件查询所获取到的数据")
    @PostMapping("conditionQueryBanner/{currentPage}/{limit}")
    public R conditionQueryBannerList(
            @ApiParam(name = "currentPage" , value = "当前页码" , required = true)
            @PathVariable Long currentPage,

            @ApiParam(name = "limit" , value = "页面记录数", required = true)
            @PathVariable Long limit){
        // 1.分页组件，其中存储了所有查询到的记录 && 同时对齐进行分页处理;
        Page<CrmBanner> bannerPage = new Page<>(currentPage, limit);
        // 2.传入分页组件 && 查询条件QueryWrapper（此处由于查询条件为空，所以传入的QueryWrapper是null;）
        crmBannerService.conditionQueryPageBanner(bannerPage);
        // 3. 将page对象中所存储的数据封装到map对象中，将其作为data进行响应返回;
        List<CrmBanner> records = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        Map<String , Object> map = new HashMap<>();
        map.put("records" , records);
        map.put("total" , total);
        return R.ok().data(map);
    }

    @ApiOperation(value = "存储Banner数据")
    @PostMapping("saveBanner")
    public R saveBanner(
            @ApiParam(name = "crmBanner" , value = "Banner数据",required = true)
            @RequestBody CrmBanner crmBanner){
        boolean isSave = crmBannerService.save(crmBanner);
        if(!isSave){
            throw new GuliExcepiton(20001 , "存储该Banner数据失败!!!");
        }
        return R.ok();
    }

    @ApiOperation(value = "修改Banner数据")
    @PostMapping("updateBanner")
    public R updateBanner(
            @ApiParam(name = "crmBanner" , value = "Banner数据",required = true)
            @RequestBody CrmBanner crmBanner){
        boolean isUpdate = crmBannerService.updateById(crmBanner);
        if(!isUpdate){
            throw new GuliExcepiton(20001 , "修改该Banner数据失败!!!");
        }
        return R.ok();
    }

    @ApiOperation(value = "查询Banner数据")
    @GetMapping("queryBanner/{bannerId}")
    public R queryBanner(
            @ApiParam(name = "bannerId" , value = "BannerID",required = true)
            @PathVariable String bannerId){
        CrmBanner banner = crmBannerService.getById(bannerId);
        if(banner == null){
            throw new GuliExcepiton(20001 , "该Banner数据不存在!!!");
        }
        return R.ok().data("banner" , banner);
    }

    @ApiOperation(value = "删除Banner数据")
    @DeleteMapping("deleteBanner/{bannerId}")
    public R deleteBanner(
            @ApiParam(name = "bannerId" , value = "BannerID",required = true)
            @PathVariable String bannerId){
        boolean isDeleted = crmBannerService.removeById(bannerId);
        if(!isDeleted){
            throw new GuliExcepiton(20001 , "删除该Banner数据失败!!!");
        }
        return R.ok();
    }

}

